
import 'package:flutter/foundation.dart';
enum LogModel {
  debug,
  warning,
  info,
  error,
}

void YLog(Object message, {LogModel model = LogModel.debug}) {
  if (kReleaseMode) {
    //release模式不打印
    return;
  }
  final info = StaticInfo.current();
  var prefix = "";
  switch(model) {
    case LogModel.debug:
      prefix = "[🟢DEBUG:${info.line}]";
      break;
    case LogModel.warning:
      prefix = "[⚠️WARNING:${info.line}]";
      break;
    case LogModel.info:
      prefix = "[🟡INFO:${info.line}]";
      break;
    case LogModel.error:
      prefix = "[🚫ERROR:${info.line}]";
      break;
  }
  final now = DateTime.now();
  print("${prefix}:${now}:[${info.fileName}]:[${info.funcName}]:${message}");
}

class StaticInfo {
  String? funcName;
  String? fileName;
  String? line;
  String? column;
  StaticInfo.current() {
    final trace = StackTrace.current;
    final traces = trace.toString().split("\n");
    for (int i = 0; i < traces.length; i++) {
      final String info = traces[i];
      if (info.contains("YLog")) {
        final result = traces[i + 1];
        final start = result.indexOf("(");
        final end = result.indexOf(")");
        // 方法名
        funcName = result.substring(0, start).trim().split(" ").last;
        var tempFileName = result.substring(start + 1, end);
        final index = tempFileName.indexOf(RegExp(r'[A-Za-z_]+.dart'));
        // 文件名
        fileName = tempFileName.substring(index).split(":").first;
        // 行号
        line = tempFileName.substring(index).split(":")[1];
        // 列号
        column = tempFileName.substring(index).split(":")[2];
        break;
      }
    }
  }
}
